package com.ruoyi.fyhs.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.fyhs.domain.lld.KtLld;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.fyhs.mapper.KtMapper;
import com.ruoyi.fyhs.domain.Kt;
import com.ruoyi.fyhs.service.IKtService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

/**
 * 费用核算课题Service业务层处理
 *
 * @author huacai
 * @date 2024-04-16
 */
@Service
public class KtServiceImpl implements IKtService
{
    @Autowired
    private KtMapper ktMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询费用核算课题
     *
     * @param ktId 费用核算课题主键
     * @return 费用核算课题
     */
    @Override
    public Kt selectKtByKtId(Integer ktId)
    {
        return ktMapper.selectKtByKtId(ktId);
    }

    /**
     * 查询费用核算课题列表
     *
     * @param kt 费用核算课题
     * @return 费用核算课题
     */
    @Override
    public List<Kt> selectKtList(Kt kt)
    {
        return ktMapper.selectKtList(kt);
    }

    /**
     * 新增费用核算课题
     *
     * @param kt 费用核算课题
     * @return 结果
     */
    @Transactional
    @Override
    public int insertKt(Kt kt)
    {
        kt.setCreateTime(DateUtils.getNowDate());
        int rows = ktMapper.insertKt(kt);
        insertKtLld(kt);
        return rows;
    }

    /**
     * 批量新增费用核算课题
     *
     * @param kts 费用核算课题List
     * @return 结果
     */
    @Override
    public int batchInsertKt(List<Kt> kts)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(kts)) {
            try {
                for (int i = 0; i < kts.size(); i++) {
                    int row = ktMapper.insertKt(kts.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == kts.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改费用核算课题
     *
     * @param kt 费用核算课题
     * @return 结果
     */
    @Transactional
    @Override
    public int updateKt(Kt kt)
    {
        kt.setUpdateTime(DateUtils.getNowDate());
        ktMapper.deleteKtLldByKtId(kt.getKtId());
        insertKtLld(kt);
        return ktMapper.updateKt(kt);
    }

    /**
     * 批量删除费用核算课题
     *
     * @param ktIds 需要删除的费用核算课题主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteKtByKtIds(Integer[] ktIds)
    {
        ktMapper.deleteKtLldByKtIds(ktIds);
        return ktMapper.deleteKtByKtIds(ktIds);
    }

    /**
     * 删除费用核算课题信息
     *
     * @param ktId 费用核算课题主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteKtByKtId(Integer ktId)
    {
        ktMapper.deleteKtLldByKtId(ktId);
        return ktMapper.deleteKtByKtId(ktId);
    }

    /**
     * 新增费用核算领料单信息
     *
     * @param kt 费用核算课题对象
     */
    public void insertKtLld(Kt kt)
    {
        List<KtLld> ktLldList = kt.getKtLldList();
        Integer ktId = kt.getKtId();
        if (StringUtils.isNotNull(ktLldList))
        {
            List<KtLld> list = new ArrayList<KtLld>();
            for (KtLld ktLld : ktLldList)
            {
                ktLld.setKtId(ktId);
                list.add(ktLld);
            }
            if (list.size() > 0)
            {
                ktMapper.batchKtLld(list);
            }
        }
    }
}
